﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using Newtonsoft.Json;

namespace PromotionEngine
{
    public partial class PromotionItemPoint : System.Web.UI.Page
    {
        public class ResponseDetail
        {
            public List<Model.PromotionItemPoint> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public ResponseDetail(List<Model.PromotionItemPoint> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public ResponseDetail(string errors)
            {
                this.Errors = errors;
            }

            public ResponseDetail()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string propertyID = Request.QueryString["PromotionID"];
            string propertyCode = Request.QueryString["PromotionCode"];
            string propertyName = Request.QueryString["PromotionName"];

            this.lblPromotionCode.Text = propertyCode;
            this.lblPromotionName.Text = propertyName;
            this.lblPromotionID.Value = propertyID;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static ResponseDetail GetItemPoint(string promotionID)
        {
            List<Model.PromotionItemPoint> lstItem = new List<Model.PromotionItemPoint>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllPromotionItemPointByPromotionID(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), promotionID);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.PromotionItemPoint item = new Model.PromotionItemPoint();
                    item.LineID = r["ID"].ToString();
                    item.PromotionID = r["PromotionID"].ToString();
                    item.SourceType = r["SourceType"].ToString();
                    item.SourceValue = r["SourceValue"].ToString();
                    item.GetPoint = double.Parse(r["GetPoint"].ToString());
                    item.BasedType = r["BasedType"].ToString();
                    item.BasedValue = double.Parse(r["BasedValue"].ToString());
                    item.ItemSourceTypeName = r["ItemSourceTypeName"].ToString();
                    item.BasedTypeName = r["BasedTypeName"].ToString();
                    item.SourceValueName = r["SourceValueName"].ToString();
                    lstItem.Add(item);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstItem };
                return new ResponseDetail(lstItem, dsPromotion.Tables[0].Rows.Count);
            }
            return new ResponseDetail();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateItemPoint(string oPromotionItem, bool isInsert)
        {
            Model.PromotionItemPoint item = JsonConvert.DeserializeObject<Model.PromotionItemPoint>(oPromotionItem);
            if (item != null)
            {
                if (item.SourceType == "3")
                {
                    item.SourceValue = item.SourceValueName;
                }
                Promotion pmt = new Promotion();
                string errMsg = pmt.UpdateJsonPromotionItemPoint(item.LineID, item.PromotionID, item.SourceType, item.SourceValue, item.GetPoint, item.BasedValue, item.BasedType,
                                                        ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static void DeleteItemPoint(string lineID)
        {
            Promotion pmt = new Promotion();
            string errMsg = pmt.DeleteJsonPromotionItemPoint(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), lineID);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReferenceValueItemSourceType()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "ItemSourceType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);

            }
            return string.Empty;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetAllReferenceValueBasedType()
        {
            List<Model.ReferenceTypeValue> lstReferenceValue = new List<Model.ReferenceTypeValue>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetReferenceValue(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), "BasedType");
            if (dsPromotion != null && dsPromotion.Tables.Count > 0)
            {
                return JsonConvert.SerializeObject(dsPromotion.Tables[0]);

            }
            return string.Empty;
        }
    }
}